// pages/address/address.js
var api = require('../../api.js');
var app = getApp();
var type = 0
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address_list: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    type = options.type
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.initData();
  },

  initData: function() {
    var page = this;
    var access_token = wx.getStorageSync("access_token");
    wx.showNavigationBarLoading();
    app.request({
      url: api.user.address_list,
      data: {
        userid: access_token,
        pageSize: 15
      },
      success: function(res) {
        console.log(res)
        wx.hideNavigationBarLoading();
        if (res.code == 200) {
          page.setData({
            address_list: res.data,
          });
        }
        page.setData({
          show_no_data_tip: (page.data.address_list.length == 0),
        });
      }
    });
  },

  setDefaultAddress: function(e) {
    var page = this;
    var index = e.currentTarget.dataset.index;
    var address = page.data.address_list[index];
    wx.showLoading({
      title: "正在保存",
      mask: true,
    });
    app.request({
      url: api.user.address_set_default,
      data: {
        id: address.id,
      },
      success: function(res) {
        wx.hideLoading();
        if (res.code == 200) {
          var address_list = page.data.address_list;
          for (var i in address_list) {
            if (i == index) {
              address_list[i].defaultStatus = 1;
            } else {
              address_list[i].defaultStatus = 0;
            }
          }
          page.setData({
            address_list: address_list,
          });
          if (type == 1) {
            wx.navigateBack()
          }
        }
      }
    });
  },

  editAddressTap: function(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },

  deleteAddressTap: function(e) {
    var page = this;
    var address_id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    wx.showModal({
      title: "提示",
      content: "确认删除改收货地址?",
      success: function(res) {
        if (res.confirm) {
          page.deleteAddress(address_id);
        }
      }
    });
  },
  formSubmit: function (e) {
    var page = this;
    var user_info = wx.getStorageSync("user_info");
    var my_info = wx.getStorageSync("my_info");
    console.log(my_info)

    page.data.form = e.detail.value;
    console.log(page.data.agree);
    var test = e.detail.value;
    console.log(wx.getStorageSync("my_info"))
    test.wxId = wx.getStorageSync("access_token")
    console.log("test")
    console.log(test)
    var access_token = wx.getStorageSync("access_token");
    wx.showNavigationBarLoading();
    app.request({
      url: api.user.address_list,
      data: {
        userid: access_token,
        key: e.detail.value.search,
        pageSize: 15
      },
      success: function (res) {
        console.log(res)
        wx.hideNavigationBarLoading();
        if (res.code == 200) {
          page.setData({
            address_list: res.data,
          });
        }
        page.setData({
          show_no_data_tip: (page.data.address_list.length == 0),
        });
      }
    });

  },

  deleteAddress: function(addressId) {
    var page = this;
    wx.showLoading({
      title: "正在删除",
      mask: true,
    });
    app.request({
      url: api.user.address_delete,
      data: {
        id: addressId,
      },
      success: function(res) {
        wx.hideLoading();
        if (res.code == 200) {
          page.initData();
        }
        if (res.code != 200) {
          wx.showToast({
            title: res.msg,
            image: "/images/icon-warning.png",
          });
        }
      }
    });
  }
});